<template>
  <div>
    <el-container>
      <el-aside width="300px"
                style="width: auto !important;padding:0px 0px;margin-bottom: 0px;background-color:#FFFFFF ">
        <el-input
            placeholder="输入部门名称进行过滤"
            prefix-icon="el-icon-search"
            v-model="filterDeptTreeText">
        </el-input>
        <el-tree :data="deptTree"
                 class="filter-tree"
                 :props="deptTreeProps"
                 default-expand-all
                 @node-click="handleNodeClick"
                 :filter-node-method="filterNode"
                 ref="tree" style="margin-top: 20px">
        </el-tree>
      </el-aside>
      <el-main class="main" style="width: auto !important;padding:0px 20px;margin-bottom: 0px;">
        <div>
          <div>
            <el-row :gutter="20">
              <el-col :span="3">
                <el-input v-model="search.username" placeholder="输入用户名称搜索"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input v-model="search.realName" placeholder="输入真实姓名搜索"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input v-model="search.mobile" placeholder="输入电话号码搜索"></el-input>
              </el-col>
              <el-col :span="3">
                <el-select v-model="search.userStatus" placeholder="选择用户状态">
                  <el-option :key="item.value" :value="item.value" :label="item.label"
                             v-for="item in userStatusOptions"/>
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-button type="success" icon="el-icon-search" @click="handlerQuery">搜索</el-button>
                <el-button type="warning" icon="el-icon-refresh-right" @click="resetHandlerQuery">重置</el-button>
              </el-col>
            </el-row>
          </div>
          <div style="margin-top: 20px">
            <el-button type="primary" icon="el-icon-plus" @click="addDept">新增</el-button>
          </div>
        </div>
        <el-dialog
            :title="dialogTitle"
            :visible.sync="dialogVisible"
            width="35%"
            :before-close="handleClose">
          <el-form ref="form" :model="form" label-width="100px">

            <el-row>
              <el-col :span="12">
                <el-form-item label="用户名">
                  <el-input v-model="form.username"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="真实姓名">
                  <el-input v-model="form.realName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="电话">
                  <el-input v-model="form.mobile"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="邮箱">
                  <el-input v-model="form.email"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="部门">
                  <el-cascader :options="deptTree" :show-all-levels="false" v-model="form.deptId"
                               :props="optionProps">
                  </el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="省">
                  <el-select v-model="form.provinceId" placeholder="请选择省">
                    <el-option :key="item.provinceId" :value="item.provinceId" :label="item.name"
                               v-for="item in provinceOptions"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="性别">
                  <el-radio-group v-model="form.sex">
                    <el-radio :label="0">女</el-radio>
                    <el-radio :label="1">男</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="用户状态">
                  <el-select v-model="form.userStatus" placeholder="请选择用户状态">
                    <el-option :key="item.value" :value="item.value" :label="item.label"
                               v-for="item in userStatusOptions"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="密码">
                  <el-input placeholder="请输入密码" v-model="form.password" show-password></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="角色">
                  <el-select v-model="form.roleIds" multiple placeholder="请选择角色">
                    <el-option
                        v-for="item in roleOptions"
                        :key="item.roleId"
                        :label="item.roleName"
                        :value="item.roleId">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handlerAddOrModifyAdmin">确 定</el-button>
              </span>
        </el-dialog>
        <div style="margin-top: 20px">
          <el-table
              :data="tableData"
              style="width: 100%"
              row-key="adminId"
              highlight-current-row
              @current-change="handleCurrentChange"
              border>
            <el-table-column
                prop="username"
                label="用户名">
            </el-table-column>
            <el-table-column
                prop="realName"
                label="真实姓名">
            </el-table-column>
            <el-table-column
                prop="sex"
                label="性别"
                align="center"
                width="60"
                :formatter="sexFormatter">
            </el-table-column>
            <el-table-column
                prop="mobile"
                label="电话">
            </el-table-column>
            <el-table-column
                prop="email"
                label="邮箱">
            </el-table-column>
            <el-table-column
                prop="province"
                label="省"
                align="center"
                width="100">
            </el-table-column>
            <el-table-column
                prop="userStatus"
                label="用户状态"
                align="center"
                width="80">
              <template slot-scope="scope">
                <el-tag
                    v-show="scope.row.userStatus === 2"
                    type="success"
                    disable-transitions>正常
                </el-tag>
                <el-tag
                    v-show="scope.row.userStatus === 1"
                    type="warning"
                    disable-transitions>锁定
                </el-tag>
                <el-tag
                    v-show="scope.row.userStatus === 3"
                    type="info"
                    disable-transitions>注销
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
                prop="deptName"
                label="部门"
                align="center">
            </el-table-column>
            <el-table-column
                prop="createDateTime"
                label="创建时间"
                align="center"
                width="150">
            </el-table-column>
            <el-table-column
                label="操作"
                width="180"
                align="center"
            >
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle @click="editDept(scope.row)"
                           size="mini"></el-button>
              </template>
            </el-table-column>
          </el-table>
          <div style="margin-top: 20px">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handlePageChange"
                :current-page="page.pageNum"
                :page-sizes="page.pageSizes"
                :page-size="page.pageSize"
                layout="total, prev, pager, next, sizes"
                :total="page.total">
            </el-pagination>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "admin",
  data() {
    return {
      deptTreeProps: {
        children: 'children',
        label: 'deptName'
      },
      deptTree: [],
      filterDeptTreeText: '',
      userStatusOptions: [
        {value: 2, label: '正常'},
        {value: 1, label: '锁定'},
        {value: 3, label: '注销'},
      ],
      provinceOptions: [
        {provinceId: 1, name: '北京市'},
        {provinceId: 2, name: '四川省'},
      ],
      roleOptions: [],
      roleDatas: [],
      page: {
        pageSize: 10,
        total: 0,
        pageNum: 1,
        pageSizes: [1, 10, 20, 50, 100]
      },
      search: {
        deptId: null,
        username: null,
        realName: null,
        mobile: null,
        provinceId: null,
        userStatus: null,
        pageNum: null,
        pageSize: null
      },
      optionProps: {
        value: 'deptId',
        label: 'deptName',
        emitPath: false,
        checkStrictly: true
      },
      form: {
        adminId: null,
        deptId: null,
        deptName: null,
        username: null,
        realName: null,
        mobile: null,
        email: null,
        provinceId: null,
        sex: null,
        userStatus: null,
        roleIds: []
      },
      dialogVisible: false,
      dialogTitle: '新增部门',
      tableData: []
    }
  },
  created() {
    this.handlerDeptTree();
    this.handlerTable();
    this.handlerRole();
    this.initProvinces();
  },
  watch: {
    filterDeptTreeText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    initProvinces() {
      var param = {};
      this.systemRequest.getProvinces(this, param, (res) => {
        if (res.data) {
          this.provinceOptions = res.data;
        }
      })
    },
    handlerRole() {
      var param = {};
      this.systemRequest.getAllRoles(this, param, (res) => {
        console.log(res)
        if (res.data) {
          this.roleOptions = res.data;
        }
      })
    },
    // 初始化部门树
    handlerDeptTree() {
      var param = {};
      this.systemRequest.deptTree(this, param, (res) => {
        console.log(res)
        if (res.data) {
          this.deptTree = res.data;
        }
      })
    },
    // 部门搜索
    filterNode(value, data) {
      if (!value) return true;
      return data.deptName.indexOf(value) !== -1;
    },
    // 点击部门树节点事件
    handleNodeClick(data) {
      this.search.deptId = data.deptId;
      this.handlerTable();
    },
    // 管理员列表数据
    handlerTable() {
      this.search.pageSize = this.page.pageSize;
      this.search.pageNum = this.page.pageNum;
      var param = this.search;
      this.systemRequest.getAdmins(this, param, (res) => {
        console.log(res)
        if (res.data) {
          this.tableData = res.data.list;
          this.page.total = res.data.total;
        } else {
          this.tableData = [];
        }
      })
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      console.log(val)
    },
    handlerQuery() {
      this.handlerTable();
    },
    resetHandlerQuery() {
      this.search.deptId = null;
      this.search.username = null;
      this.search.realName = null;
      this.search.mobile = null;
      this.search.provinceId = null;
      this.search.userStatus = null;
      this.handlerTable();
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(() => {
            done();
          }).catch(() => {
      });
    },
    addDept() {
      this.dialogTitle = '新增管理员';
      this.dialogVisible = true;

      this.form.adminId = null;
      this.form.deptId = null;
      this.form.deptName = null;
      this.form.username = null;
      this.form.realName = null;
      this.form.mobile = null;
      this.form.email = null;
      this.form.provinceId = null;
      this.form.sex = null;
      this.form.userStatus = null;
      this.form.roleIds = [];
    },
    editDept(val) {
      this.dialogTitle = '编辑部门';
      this.dialogVisible = true;

      this.form.adminId = val.adminId;
      this.form.deptId = val.deptId;
      this.form.deptName = val.deptName;
      this.form.username = val.username;
      this.form.realName = val.realName;
      this.form.mobile = val.mobile;
      this.form.email = val.email;
      this.form.provinceId = val.provinceId;
      this.form.sex = val.sex;
      this.form.userStatus = val.userStatus;
      this.form.roleIds = val.roleIds;

    },
    /**
     * 添加或修改部门
     */
    handlerAddOrModifyAdmin() {
      var param = this.form;
      if (this.form.adminId) {
        this.systemRequest.modifyAdmin(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("管理员信息已修改");
          this.handlerTable();
        })
      } else {
        this.systemRequest.addAdmin(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("管理员已添加");
          this.handlerTable();
        })
      }
    },
    deleteHandleClose(index) {
      this.$refs[`popover-${index}`].doClose()
    },
    // 性别格式化
    sexFormatter(row) {
      if (row.sex === 0) {
        return '女';
      } else {
        return '男';
      }
    },
    // 页大小发生改变事件
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.handlerTable();
    },
    // 页码发生改变事件
    handlePageChange(val) {
      this.page.pageNum = val;
      this.handlerTable();
    }
  },
}
</script>

<style scoped>

</style>